#!/bin/bash

# set -ux

echo '配置ssh免密'
if [ -f "/root/.ssh" ]; then
  echo "/root/.ssh exist"
else
  echo "/root/.ssh does not exist"
  ssh-keygen -t rsa
  cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
fi

echo '配置java'
mkdir -p /usr/local/java &&
  tar -zxf res/Alibaba_Dragonwell_8.10.11_x64_linux.tar.gz -C /usr/local/java

echo '配置zookeeper'
tar -zxf res/apache-zookeeper-3.8.0-bin.tar.gz &&
  mv apache-zookeeper-3.8.0-bin /home/zookeeper &&
  mkdir -pv /home/zookeeper/{data,logs} &&
  cp /home/zookeeper/conf/zoo_sample.cfg /home/zookeeper/conf/zoo.cfg

# 默认id
echo 1 > /home/zookeeper/data/myid

sed -i "/^dataDir=/c dataDir=/home/zookeeper/data" /home/zookeeper/conf/zoo.cfg

cat >> /home/zookeeper/conf/zoo.cfg << EOF
dataLogDir=/home/zookeeper/logs
# server cluster
server.1=server160:2888:3888
server.2=server161:2888:3888
server.3=server162:2888:3888
EOF

cat /home/zookeeper/conf/zoo.cfg | grep -v '^#' | grep -v '^$'

echo '配置hadoop'
tar -zxf res/hadoop-3.3.1.tar.gz &&
  mv hadoop-3.3.1 /home/hadoop &&
  cp -f config/* /home/hadoop/etc/hadoop &&
  mkdir -pv /home/hadoop/{tmp,logs,hdfs} &&
  mkdir -pv /home/hadoop/hdfs/{journal,name,data}

cat > /home/hadoop/etc/hadoop/workers << EOF
server160
server161
server162
EOF

echo '配置hdp环境'
cat >> /home/hadoop/etc/hadoop/hadoop-env.sh << EOF
export HDFS_NAMENODE_USER=root
export HDFS_SECONDARYNAMENODE_USER=root
export HDFS_DATANODE_USER=root
export HDFS_DATANODE_SECURE_USER=root
export HDFS_JOURNALNODE_USER=root
export HDFS_ZKFC_USER=root
export HADOOP_SHELL_EXECNAME=root
export HADOOP_COMMON_LIB_NATIVE_DIR=${HADOOP_HOME}/lib/native
export HADOOP_OPTS="-Djava.library.path=${HADOOP_HOME}/lib/"
EOF

cat /home/hadoop/etc/hadoop/hadoop-env.sh  | grep -v '^#' | grep -v '^$'

echo '配置yarn环境'
cat >> /home/hadoop/etc/hadoop/yarn-env.sh << EOF
export YARN_NODEMANAGER_USER=root
export YARN_RESOURCEMANAGER_USER=root
export HADOOP_SECURE_DN_USER=root
EOF

cat /home/hadoop/etc/hadoop/yarn-env.sh  | grep -v '^#' | grep -v '^$'

echo '配置环境变量'
cat >> ~/.bashrc << EOF
# java env
export JAVA_HOME=/usr/local/java/dragonwell-8.10.11
export PATH=\$PATH:\$JAVA_HOME/bin
# zookeeper env
export ZOOKEEPER_HOME=/home/zookeeper
export PATH=\$PATH:\$ZOOKEEPER_HOME/bin
# hadoop env
export HADOOP_HOME=/home/hadoop
export PATH=\$PATH:\$HADOOP_HOME/bin:\$HADOOP_HOME/sbin
EOF

cat ~/.bashrc | grep -v '^#' | grep -v '^$'
